/**
 * 质控病种列表初始化
 */
var SdsQcentity = {
    id: "entityTree",	//表格id
    seItem: null,		//选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    entityId: 0
};

SdsQcentity.onClickEntity = function (e, treeId, treeNode) {
    if (treeNode.pId == null || treeNode.pId == 0) {
        Sys.info("不能选择根节点（"+treeNode.name+"），请重新选择具体病种。");
        return false;
    }
    SdsQcentity.entityId = treeNode.id;
    //查询质控项目
    SdsQcentityRule.clearData();
    SdsQcentityRule.search();
};

/**
 * 病种筛查配置管理初始化
 */
var SdsQcentityRule = {
    id: "SdsQcentityRuleTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
SdsQcentityRule.initColumn = function () {
    return [
        {field: 'selectItem', radio: true, visible: false},
        {field: 'indNo', width: 1, align: 'center', valign: 'middle', sortable: false},
        {title: '病种ID', field: 'entityId', width: 1, align: 'center', valign: 'middle', sortable: false, visible: false},
        {title: '项目类型', field: 'ruleType', width: 2, align: 'center', valign: 'middle', sortable: false, formatter:function(value,row,index) {
                if (value == 1) {
                    return "准入";
                } else {
                    return "排除";
                }
            }},
        {title: '是否有效', field: 'isActive', width: 2, align: 'center', valign: 'middle', sortable: false, formatter:function(value,row,index) {
                if (value == 1) {
                    return "<span class='label label-success'>是</span>";
                } else {
                    return "<span class='label label-warning'>否</span>";
                }
            }},
        {title: '仅在院用', field: 'isOnly', width: 2, align: 'center', valign: 'middle', sortable: false, formatter:function(value,row,index) {
                if (value == 1) {
                    return "<span class='label label-success'>是</span>";
                } else {
                    return "<span class='label label-warning'>否</span>";
                }
            }},
        {title: '筛查描述', field: 'ruleDesc', width: 8, align: 'left', valign: 'middle', sortable: false},
        {title: '筛查函数', field: 'expressId', width: 3, align: 'center', valign: 'middle', sortable: false,  formatter:function(value,row,index) {
                var expObj = $("#expressId option");
                var expDesc = "";
                expObj.each(function(i){
                    if(value == expObj[i].value) {
                        expDesc =  expObj[i].innerHTML;
                    }
                });
                return expDesc;
            }},
        {title: '筛查入参', field: 'param', width: 3, align: 'center', valign: 'middle', sortable: false},
        {title: '预留参数', field: 'param2', width: 3, align: 'center', valign: 'middle', sortable: false}];
};

/**
 * 选中规则行填充上方控件
 */
SdsQcentityRule.fillData = function (obj,$ele,field) {
    if ($($ele)[0].className == 'info') {
        SdsQcentityRule.clearData();
        $($ele).removeClass('info');
    } else {
        SdsQcentityRule.seItem = obj;
        $('#ruleType').combobox('setValue', obj.ruleType);
        $('#ruleDesc').val(obj.ruleDesc);
        $('#expressId').combobox('setValue', obj.expressId);
        $('#param').val(obj.param);
        $('#param2').val(obj.param2);
        $('#isActive').checkbox('setValue', obj.isActive == 1 ? true : false);
        $('#isOnly').checkbox('setValue', obj.isOnly == 1 ? true : false);
        // 改变选中行背景色
        $('#SdsQcentityRuleTable .info').removeClass('info');//移除class
        $($ele).addClass('info');//添加class
    }
}

/**
 * 清除上方控件数据
 */
SdsQcentityRule.clearData = function ( ) {
    SdsQcentityRule.seItem = null;
    $('#ruleType').combobox('clear');
    $('#ruleDesc').val('');
    $('#expressId').combobox('clear');
    $('#param').val('');
    $('#param2').val('');
    $('#isActive').checkbox('clear');
    $('#isOnly').checkbox('clear');
    // 改变选中行背景色
    $('#SdsQcentityRuleTable .info').removeClass('info');//移除class
}

/**
 * 检查是否选中病种
 */
SdsQcentityRule.check = function () {
        if (SdsQcentity.entityId) {
            return true;
        } else {
            Sys.info("请先选中某一病种！");
            return false;
        }
};

/**
 * 验证数据是否为空
 */
SdsQcentityRule.validate = function () {
    var errinfo = "";
    if ($("#ruleType").combobox("getValue")=="") {
        errinfo = errinfo + "请选择规则类型!<br>";
    }
    if ($("#ruleDesc").val()=="") {
        errinfo = errinfo + "请填写描述!<br>";
    }
    if (errinfo) {
        $.messager.alert("错误提示", errinfo, 'info');
        return false;
    }
    return true;
}

/**
 * 编辑筛查配置
 */
SdsQcentityRule.edit = function (id) {
	var flag = false;
	if (this.check()) {
        flag = true;
    }
	if (flag) {
        if (!this.validate()) {
            return;
        }
        var queryData = {};
        queryData['id'] = SdsQcentityRule.seItem!==null?SdsQcentityRule.seItem.id:null;
        queryData['entityId'] = SdsQcentityRule.seItem!==null?SdsQcentityRule.seItem.entityId:SdsQcentity.entityId;
        queryData['ruleType'] = $("#ruleType").combobox("getValue");
        queryData['ruleDesc'] = encodeURIComponent($("#ruleDesc").val());
        queryData['expressId'] = $("#expressId").combobox("getValue");
        queryData['param'] = encodeURIComponent($("#param").val());
        queryData['param2'] = encodeURIComponent($("#param2").val());
        queryData['isActive'] = $("#isActive").checkbox("getValue")?1:0;
        queryData['isOnly'] = $("#isOnly").checkbox("getValue")?1:0;
        var rd = 1;
        if (SdsQcentityRule.seItem) {
            rd = SdsQcentityRule.seItem.indNo;
        } else {
            rd = $("#SdsQcentityRuleTable").bootstrapTable("getOptions").totalRows + 1
        }
        queryData['indNo'] = rd;
        //提交信息
        var ajax = new $ax(Sys.backendPath + "/sds/sdsqcentityrule/edit", function (res) {
            Sys.success(res.message);
            SdsQcentityRule.table.refresh();
        }, function (data) {
            Sys.error("添加失败!" + res.message + "!");
        });
        ajax.setData(queryData);
        ajax.start();
    }
};

SdsQcentityRule.import = function () {
    var index = Sys.open('导入病种筛查规则', Sys.backendPath + '/sds/sdsqcentityrule/to_import', ['550px', '300px']);
    this.layerIndex = index;
};

/**
 * 查询病种筛查配置列表
 */
SdsQcentityRule.search = function () {
    var queryData = {};
    queryData['entityId'] = SdsQcentity.entityId;
    SdsQcentityRule.table.refresh({query: queryData, pageNumber: 1});
};

$(function () {
    //查询病种树
    var ztree = new $ZTree("entityTree", Sys.backendPath + "/sds/sdsqcentity/tree");
    ztree.bindOnClick(SdsQcentity.onClickEntity);
    ztree.init();
    SdsQcentity.ztree = ztree;
    fuzzySearch('entityTree','#seachKey',null,true);

    //项目规则获取
    var defaultColunms = SdsQcentityRule.initColumn();
    var table = new BSTable(SdsQcentityRule.id, Sys.backendPath + "/sds/sdsqcentityrule/list", defaultColunms);
    table.setPaginationType("server");
    table.setPageSize(14);
    table.setSubHeight(-40);
    table.setShowExport(false);
    table.setShowRefresh(false);
    table.setShowToggle(false);
    table.setShowColumns(false);
    table.setOnClickRow(SdsQcentityRule.fillData);
    table.setQueryParams({"entityId":0});
    SdsQcentityRule.table = table.init();
});
